package ckw.gui;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import ckw.database.model.Election;
import ckw.database.model.ElectionNode;
import core.utils.DialogPositioner;

public class DlgElectionState extends Dialog {
	private static class ContentProvider implements IStructuredContentProvider {
		@SuppressWarnings("unchecked")
		public Object[] getElements(Object inputElement) {
			List<ElectionNode> list = (ArrayList<ElectionNode>) inputElement;
			return list.toArray(new ElectionNode[list.size()]);
		}

		public void dispose() {
		}

		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}
	}

	protected Object result;
	protected Shell shell;

	private Election election;

	private TableViewer tblVwrElectionState;
	private Table tblElectionState;
	private TableColumn tblclmnRkwName;
	private TableViewerColumn tblVwrClmnRkwName;
	private TableColumn tblclmnState;
	private TableViewerColumn tblVwrClmnState;
	private Button btnOK;

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 */
	public DlgElectionState(Shell parent, Election election) {
		super(parent, SWT.DIALOG_TRIM | SWT.RESIZE | SWT.APPLICATION_MODAL);

		this.election = election;

		setText("Election state");
		
		shell = new Shell(getParent(), getStyle());
		shell.setSize(450, 300);
		shell.setText(getText());
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		DialogPositioner.centerShell(shell);

		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell.setLayout(new GridLayout(1, false));

		tblVwrElectionState = new TableViewer(shell, SWT.BORDER | SWT.FULL_SELECTION);
		tblElectionState = tblVwrElectionState.getTable();
		tblElectionState.setHeaderVisible(true);
		tblElectionState.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		tblVwrClmnRkwName = new TableViewerColumn(tblVwrElectionState, SWT.NONE);
		tblVwrClmnRkwName.setLabelProvider(new ColumnLabelProvider() {
			public String getText(Object element) {
				return element == null ? "" : ((ElectionNode) element).getNode().getName();
			}
		});
		tblclmnRkwName = tblVwrClmnRkwName.getColumn();
		tblclmnRkwName.setWidth(290);
		tblclmnRkwName.setText("RKW Name");

		tblVwrClmnState = new TableViewerColumn(tblVwrElectionState, SWT.NONE);
		tblVwrClmnState.setLabelProvider(new ColumnLabelProvider() {
			public String getText(Object element) {
				return element == null ? "" : ((ElectionNode) element).getStatus().toString();
			}
		});
		tblclmnState = tblVwrClmnState.getColumn();
		tblclmnState.setWidth(100);
		tblclmnState.setText("State");
		tblVwrElectionState.setContentProvider(new ContentProvider());
		tblVwrElectionState.setInput(election.getNodesAsSortedList());

		btnOK = new Button(shell, SWT.NONE);
		btnOK.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent event) {
				shell.close();
			}
		});
		GridData gdBtnClose = new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1);
		gdBtnClose.widthHint = 100;
		btnOK.setLayoutData(gdBtnClose);
		btnOK.setText("OK");
	}

	public void refreshElectionState(Election election) {
		if(this.election.getId().longValue() == election.getId().longValue()) {
			this.election = election;
			tblVwrElectionState.setInput(election.getNodesAsSortedList());
		}
	}

}
